/**
 *
 */
package com.igeekhome.service.impl;

import com.igeekhome.dto.SysPermissionDto;
import com.igeekhome.mapper.SysPermissionMapper;
import com.igeekhome.mapper.SysRoleMapper;
import com.igeekhome.service.ISysPermissionService;
import com.igeekhome.util.CheckArr;
import com.igeekhome.util.Dtree;
import com.igeekhome.util.NavBean;
import com.igeekhome.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;


/**
 * @author 立以宁
 */
@Service("sysPermissionService")
public class SysPermissionServiceImpl implements ISysPermissionService {
    
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    
    @Autowired
    private SysRoleMapper roleMapper;
    
    @Override
    public List<NavBean> listNavs(Integer uid) {
        List<SysPermissionDto> per = sysPermissionMapper.selectPermission(uid);
        
        
        List<NavBean> parent_nav = getChild(per, null);
        for (NavBean nav : parent_nav) {
            List<NavBean> child = getChild(per, nav.getId());
            
            for (NavBean childNav : child) {
                List<NavBean> grandchild = getChild(per, childNav.getId());
                childNav.setChildren(grandchild);
            }
            nav.setChildren(child);
        }
        return parent_nav;
    }
    
    @Override
    public ResultBean powerList() {
        List<SysPermissionDto> powers = sysPermissionMapper.selectByAll();
        List<Dtree> dtrees = returnDtree(powers);
        return ResultBean.ok(dtrees);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultBean disPower(Integer[] powers, Integer roleId) {
        // 设置回滚点
        Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
        try {
            roleMapper.delRolePower(roleId);
            for (Integer power : powers) {
                roleMapper.insertRolePower(roleId,power);
            }
            return ResultBean.ok();
        } catch (Exception e) {
            e.printStackTrace();
            // 回滚到回滚点
            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
            return ResultBean.fail("数据提交失败,已回滚");
        }
    }
    
    
    public List<NavBean> getChild(List<SysPermissionDto> per, Integer parentId) {
        List<NavBean> children = new ArrayList<>();
        
        for (SysPermissionDto sys : per) {
            if (sys.getParent_id() == null) {
                if (parentId == null) {
                    NavBean nav = new NavBean(sys.getId(), sys.getPower_name(), sys.getIcon(), sys.getUrl_address(), false, null);
                    children.add(nav);
                }
            } else if (sys.getParent_id().equals(parentId)) {
                NavBean nav = new NavBean(sys.getId(), sys.getPower_name(), sys.getIcon(), sys.getUrl_address(), false, null);
                children.add(nav);
            }
        }
        return children;
    }
    
    
    
    /**
    * @Description 往dtree树中塞值
    * @Param
    * @Return
    **/
    public List<Dtree> returnDtree(List<SysPermissionDto> powers){
        List<Dtree> dtrees = new ArrayList<>();
        for (SysPermissionDto power : powers) {
            Dtree dtree = new Dtree();
            if (power.getParent_id()==null){
                dtree.setParentId(0);
            }else{
                dtree.setParentId(power.getParent_id());
            }
            dtree.setCheckArr(new CheckArr("0","0")).setId(power.getId()).setTitle(power.getPower_name());
            dtrees.add(dtree);
        }
        return dtrees;
    }
}
